/*
 *说明：资源账号信息表 首页脚本
 *
 *@author liuxun 2012-03-18
*/

var opeForm;
var grid;

$(function(){
	opeForm = $('#opeForm');
	$("#tabs").tabs({
		onSelect : function(title){
			parent.selectedTab = title;	//记录当前选中的tab页签 
			if(title == '接管账号管理'){
				if (!isEmpty($('#listFrame').attr('src'))) {
					return;
				}
				queryOperation();
			}else if(title == '接管账号下拉'){
				if (!isEmpty($('#pullAccountFrame').attr('src'))){
					return;
				}
				$('#pullAccountFrame').attr('src','C_RSC_ACC!showPullIndex.do');
			}else if(title == '选择形式下拉'){
				if (!isEmpty($('#selectFrame').attr('src'))){
					return;
				}
				$('#selectFrame').attr('src','C_RSC_ACC!showPullAccIndex.do');
			}
		}
		
	}); 

	grid=$('#listTable').datagrid({
		frozenColumns:[[
            {title:'选择',field:'ck',checkbox:true}
		]],
		columns:[[

            {title:'节点名称',field:'node_decode',align:'center',width:150},
            
            {title:'资源组名称',field:'rsc_grp_name',align:'center',width:150},
            
            {title:'资源名称',field:'rsc_name',align:'center',width:150},

            {title:'资源账号',field:'rsc_acc',align:'center',width:100},

            {title:'允许定期更新密码',field:'is_updte_pwd',align:'center',width:80, 
            	formatter:function(value,rec){
            		return value ==  '1' ? '<font color="green"> √ </font>' : '<font color="red"> × </font>';
  			    }
             },

            {title:'账号类型',field:'acc_type_decode',align:'center',width:80},
             
             {title:'账号索引',field:'rsc_acc_no',align:'center',width:80},
             
             {title:'账号状态',field:'rsc_acc_status_decode',align:'center',width:80},

			{title:'操作',field:'operation',align:'center',width:100,
	        	formatter:function(value,rec){
         		var s = '<a style="cursor:pointer; color:blue" onclick=\'showInfo("' + rec.key + '")\'>查看明细</a>';
	        	    return s;
		   		}
		    }
        ]],
		pagination:true,
		singleSelect: false
	});

	$('#queryBtn').click(queryOperation);
	$('#clearBtn').click(clearOperation);
	$('#reloadBtn').click(refreshWin);
	$('#createBtn').click(createOperation);
	$('#addBtn').click(addOperation);
	$('#editBtn').click(editOperation);
	$('#deleteBtn').click(deleteOperation);
	$('#printBtn').click(printOperation);
});

//翻页时自动调用此函数
function onLoadPrev(msg){
	var queryParams=grid.datagrid('options').queryParams;
	queryParams['firstQuery']=false;
}

//查询操作
function queryOperation(){
	var params = opeForm.serializeObject();
	grid.datagrid("options").pageNumber = 1;
	grid.datagrid({
		url: 'C_RSC_ACC!queryList.do',
		queryParams: jQuery.extend({}, params, {'firstQuery': true})
	});
}

//清空操作（重置）
function clearOperation() {
	opeForm.resetForm();
}

//选择资源组
function select_c_rsc_grp() {
	var node_id = $('#node_id').val();
	var action = "C_RSC_GRP!showSelect.do?model.rsc_grp_status=01&model.node_id="+node_id;
	var row = showModalWin(action,'A2');
	
	if (row) {
		$('#rsc_grp_id').val(row.rsc_grp_id);
		$('#rsc_grp_name').val(row.rsc_grp_name);
	}
}

//选择资源
function select_c_rsc() {
	var node_id = $('#node_id').val();
	var action = "C_RSC!showSelect.do?model.rsc_status=01&model.node_id=" + node_id;
	var row = showModalWin(action,'A2');
	
	if (row) {
		$('#rsc_id').val(row.rsc_id);
		$('#rsc_name').val(row.rsc_name);
		$('#node_id').val(row.node_id);
	}
}

//查看明细
function showInfo(key) {
	var url = 'C_RSC_ACC!showInfo.do?model.key=' + key;
	openWin(url,'pop'); 
}

//创建资源账号
function createOperation(){
	var url = 'C_RSC_ACC!showCreate.do';
	openWin(url, 'pop', 'A3');
}

//添加资源账号信息表
function addOperation(){  
	var url = 'C_RSC_ACC!showAdd.do';
	openWin(url,'pop','A3');
}

//编辑资源账号信息表
function editOperation(){  
	var rows = grid.datagrid('getSelections');

	if (rows.length == 0) {
		showInfoWin('请您首先选中一条记录!');
		return;
	}  

	if (rows.length > 1) {
		showInfoWin('只能选择一条记录');
      	return; 
	}
	
	if (rows[0].rsc_acc_status == '03') {
		showInfoWin('已删除的资源账号信息不允许修改操作！');
      	return; 
	}

	var url = 'C_RSC_ACC!showEdit.do?model.key=' + rows[0].key;
	openWin(url,'pop','A3');
}   

//删除资源账号信息表
function deleteOperation() {
	var rows = grid.datagrid('getSelections');

	if (rows.length == 0) {
		showInfoWin('请您首先选中要删除的记录!');	
		return;
	}

	var ids = [];
	for (var i = 0; i < rows.length; i++) {
		ids.push(rows[i].key);
	}
	var params = $ .param({'id' : ids}, true);

	$ .messager.confirm('提示信息', '您确定要删除选定的' + rows.length + '条记录吗?', function(result){
		if(result){
			$ .ajax({
				url: 'C_RSC_ACC!doDelete.do',
				data: params,
				type: 'post',
				success: function(data){
				    showInfoWin('操作成功!');
					afterOK();
				}
			});
		}	
	});
}

//打印
function printOperation() {
	 
	var rows = grid.datagrid('getSelections');  
   	
   	if (rows.length == 0) {
   		showInfoWin('请您首先选中一条记录!');
		return;
   	}

	var ids = [];
	for (var i = 0; i < rows.length; i++) {
		ids.push(rows[i].key);
	}	
	  opeForm.attr('action', 'C_RSC_ACC!showPrint.do?params=' + ids);
	  opeForm.attr('target', 'hideWindow');
	  opeForm.submit();
}

//操作成功后刷新列表
function afterOK(){
	queryOperation();
}